package com.bysj.sausys.controller.backend;

import com.bysj.sausys.beans.PageQuery;
import com.bysj.sausys.beans.PageResult;
import com.bysj.sausys.common.DicParamsConst;
import com.bysj.sausys.common.JsonData;
import com.bysj.sausys.common.UserConst;
import com.bysj.sausys.model.SysDicParams;
import com.bysj.sausys.service.ISysDicParamsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 字典Controller
 *
 * @author WangZK
 */
@Api(description = "字典管理接口")
@Controller
@RequestMapping("/sys/dic")
public class SysDicParamsController {

    @Resource
    private ISysDicParamsService sysDicParamsService;


    /**
     * 新增字典分类接口
     *
     * @param sysDicParams
     * @param request
     * @return
     */
    @ApiOperation(value = "新增字典分类接口")
    @RequestMapping(value = "/saveDicType", method = RequestMethod.POST)
    @ResponseBody
    public JsonData saveDicType(@ApiParam(value = "字典实体", required = true) @RequestBody SysDicParams sysDicParams,
                                HttpServletRequest request) {
        // 从session中获取当前用户
        Map map = UserConst.getCurrentUser(request);
        sysDicParamsService.saveDicType(sysDicParams, UserConst.getCurrentUsername(map));
        return JsonData.success();
    }

    /**
     * 修改字典分类接口
     *
     * @param sysDicParams
     * @param request
     * @return
     */
    @ApiOperation(value = "修改字典分类接口")
    @RequestMapping(value = "/updateDicType", method = RequestMethod.PUT)
    @ResponseBody
    public JsonData updateDicType(@ApiParam(value = "字典实体", required = true) @RequestBody SysDicParams sysDicParams,
                                  HttpServletRequest request) {
        // 从session中获取当前用户
        Map map = UserConst.getCurrentUser(request);
        sysDicParamsService.updateDicType(sysDicParams, UserConst.getCurrentUsername(map));
        return JsonData.success();
    }

    /**
     * 分页获取字典分类接口
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "分页获取字典分类接口")
    @RequestMapping(value = "/getDicTypeForPage", method = RequestMethod.GET)
    @ResponseBody
    public JsonData getDicTypeForPage(@ApiParam(value = "页码") @RequestParam(required = false, defaultValue = "1") int pageNo,
                                      @ApiParam(value = "每页显示条数") @RequestParam(required = false, defaultValue = "10") int pageSize) {
        PageResult result = sysDicParamsService.getDicTypeForPage(new PageQuery(pageNo, pageSize));
        return JsonData.success(result);
    }

    /**
     * 根据id获取字典表数据接口
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id获取字典表数据接口")
    @RequestMapping(value = "/getOne", method = RequestMethod.GET)
    @ResponseBody
    public JsonData getOne(@ApiParam(value = "字典id", required = true) @RequestParam Integer id) {
        SysDicParams sysDicParams = sysDicParamsService.findById(id);
        return JsonData.success(sysDicParams);
    }

    /**
     * 根据id删除字典分类接口
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id删除字典分类接口")
    @RequestMapping(value = "/deleteDicType", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonData deleteDicType(@ApiParam(value = "字典id", required = true) @RequestParam Integer id) {
        sysDicParamsService.deleteDicType(id);
        return JsonData.success();
    }

    /**
     * 根据id禁用字典表数据接口
     *
     * @param id
     * @param request
     * @return
     */
    @ApiOperation(value = "根据id禁用字典表数据接口")
    @RequestMapping(value = "/setDicParamsIsDisable", method = RequestMethod.PUT)
    @ResponseBody
    public JsonData setDicParamsIsDisable(@ApiParam(value = "字典id", required = true) @RequestParam Integer id,
                                          HttpServletRequest request) {
        Map map = UserConst.getCurrentUser(request);
        sysDicParamsService.setDicParamsStatus(id, DicParamsConst.DISABLE.getCode(), UserConst.getCurrentUsername(map));
        return JsonData.success();
    }

    /**
     * 根据id启用字典表数据接口
     *
     * @param id
     * @param request
     * @return
     */
    @ApiOperation(value = "根据id启用字典表数据接口")
    @RequestMapping(value = "/setDicParamsIsEnable", method = RequestMethod.PUT)
    @ResponseBody
    public JsonData setDicParamsIsEnable(@ApiParam(value = "字典id", required = true) @RequestParam Integer id,
                                         HttpServletRequest request) {
        Map map = UserConst.getCurrentUser(request);
        sysDicParamsService.setDicParamsStatus(id, DicParamsConst.ENABLE.getCode(), UserConst.getCurrentUsername(map));
        return JsonData.success();
    }

    /**
     * 新增字典数据接口
     *
     * @param sysDicParams
     * @param request
     * @return
     */
    @ApiOperation(value = "新增字典数据接口")
    @RequestMapping(value = "/saveDicParams", method = RequestMethod.POST)
    @ResponseBody
    public JsonData saveDicParams(@ApiParam(value = "字典实体", required = true) @RequestBody SysDicParams sysDicParams,
                                  HttpServletRequest request) {
        // 从session中获取当前用户
        Map map = UserConst.getCurrentUser(request);
        sysDicParamsService.saveDicParams(sysDicParams, UserConst.getCurrentUsername(map));
        return JsonData.success();
    }

    /**
     * 修改字典数据接口
     *
     * @param sysDicParams
     * @param request
     * @return
     */
    @ApiOperation(value = "修改字典数据接口")
    @RequestMapping(value = "/updateDicParams", method = RequestMethod.PUT)
    @ResponseBody
    public JsonData updateDicParams(@ApiParam(value = "字典实体", required = true) @RequestBody SysDicParams sysDicParams,
                                    HttpServletRequest request) {
        // 从session中获取当前用户
        Map map = UserConst.getCurrentUser(request);
        sysDicParamsService.updateDicParams(sysDicParams, UserConst.getCurrentUsername(map));
        return JsonData.success();
    }

    /**
     * 分页获取字典数据接口
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "分页获取字典数据接口")
    @RequestMapping(value = "/getDicParamsForPage", method = RequestMethod.GET)
    @ResponseBody
    public JsonData getDicParamsForPage(@ApiParam(value = "字典分类") @RequestParam(required = false) String pmappname,
                                        @ApiParam(value = "页码") @RequestParam(required = false, defaultValue = "1") int pageNo,
                                        @ApiParam(value = "每页显示条数") @RequestParam(required = false, defaultValue = "10") int pageSize) {
        PageResult result = sysDicParamsService.getDicParamsForPage(pmappname, new PageQuery(pageNo, pageSize));
        return JsonData.success(result);
    }

    /**
     * 根据字典类型获取字典数据
     *
     * @param pmappname
     * @return
     */
    @ApiOperation(value = "根据字典类型获取字典数据")
    @RequestMapping(value = "/getDicParamsByType", method = RequestMethod.GET)
    @ResponseBody
    public JsonData getDicParamsByType(@ApiParam(value = "字典类型", required = true) @RequestParam String pmappname) {
        List<SysDicParams> sysDicParamsList = sysDicParamsService.getDicParamsByPmappname(pmappname);
        return JsonData.success(sysDicParamsList);
    }

    /**
     * 获取所有启用的字典分类
     */
    @ApiOperation(value = "获取所有启用的字典分类")
    @RequestMapping(value = "/getAllDicType", method = RequestMethod.GET)
    @ResponseBody
    public JsonData getAllDicType() {
        List<SysDicParams> sysDicParamsList = sysDicParamsService.getAllDicType();
        return JsonData.success(sysDicParamsList);
    }
}
